package net.sapfree.display.tags.quan;

import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import net.sapfree.common.Constants;
import net.sapfree.core.db.DBConnectionProvider;
import net.sapfree.core.db.DataBaseExecutor;
import net.sapfree.core.db.Record;
import net.sapfree.core.db.RecordSet;
import net.sapfree.display.tags.core.LoopTagSupport;

import org.apache.log4j.Logger;

//����Ȧ�ӳ�Ա
public class MemberListTag extends LoopTagSupport {
	private static Logger log = Logger.getLogger(MemberListTag.class.getClass());
	private RecordSet memberList=null;
	private String quanuuid;	//Ȧ�ӵ���ƣ����Ȧ�Ӷ��Ÿ�
	private String modulename;	//���ҵ��ģ����Ʋ�ѯȦ�ӣ��ٸ��Ȧ�Ӳ�ѯȦ�ӳ�Ա
	private int count=-1;
	@Override
	protected boolean hasNext() throws JspException {
		return (memberList != null)&&(step<end);
	}

	@Override
	protected Object next() throws JspException {
		Record r=memberList.get(step);
		step++;
		return r;
	}

	 
	
	@Override
	protected void prepare() throws JspException {
		ServletRequest request = pageContext.getRequest();
		Connection conn =null;

		try {
			conn = DBConnectionProvider.getConnection(Constants.JNDI_SAPFREE);
			DataBaseExecutor dbe = DataBaseExecutor.getExecutor(conn);
			String getquanuuid = "";
			
			if(modulename!=null && !"".equals(modulename.trim())){
				RecordSet quanList = dbe.find(getQuanListSql());
				System.out.println("--------------- "+getQuanListSql());
				if(quanList!=null && quanList.size()>0){
					for(int q=0;q<quanList.size();q++){
						Record quan = quanList.get(q);
						getquanuuid += quan.getString("QUAN_UUID")+",";
					}
				}
			}
			
			quanuuid += getquanuuid;
			System.out.println("quan uuid "+quanuuid);
			
			memberList=dbe.find(getPersonListSql());
			System.out.println("----------- "+getPersonListSql());
			System.out.println("------memberlist "+memberList);
			pageContext.setAttribute("MemberListTag", this, PageContext.PAGE_SCOPE);
			pageContext.setAttribute("MemberList", this.getMemberList(), PageContext.PAGE_SCOPE);
			end=memberList.size();
		 	step=0;
		} catch (SQLException e) {
			log.error("��ִ������Ա�б�ʱ���",e);
		}finally{
			try{
				 conn.close();
			}catch(Exception e){}
		}
	}
	
	private String getQuanListSql(){
		if(modulename==null || "".equals(modulename.trim())){
			return "";
		}
		String[] modulenames = modulename.split(",");
		StringBuffer sql = new StringBuffer();
		
		sql.append("SELECT * FROM QUANINFO i where " );
		for(int i=0;i<modulenames.length;i++){
			sql.append(" i.quan_name like '%").append(modulenames[i]).append("%' OR ");
		}
		sql.delete(sql.length()-3, sql.length());
		
		if(count!=-1){
			sql.append(" limit 0,").append(count);
		}
		return sql.toString();
	}
	
	private String getPersonListSql(){
		String[] quanuuids =quanuuid.split(",");
		StringBuffer sql=new StringBuffer();
		//���û������������򷵻����еļ�¼
		sql.append("SELECT distinct PI_UUID,PI_EMAIL,PI_FIRSTNAME,PI_LASTNAME,PI_ZHAOPIAN,PI_YSZHAOPIAN FROM QUANPERSON,PERSONINFO where PI_UUID=QP_PERSONUUID " );
		sql.append(" and ( ");
		for(int i=0;i<quanuuids.length;i++){
				sql.append(" qp_quanuuid = '"+quanuuids[i]+"' OR ");
		}
		sql.delete(sql.length()-3, sql.length());//ɾ������OR
		sql.append(" )");
		
		if(count!=-1){
			sql.append(" limit 0,").append(count);
		}
		
		System.out.println("----quan memeber sql "+sql);
		return sql.toString();
	}

	/**
	 * @return the filterName
	 */
	public String getQuanuuid() {
		return quanuuid;
	}

	/**
	 * @param filterName the filterName to set
	 */
	public void setQuanuuid(String quanuuid) {
		this.quanuuid = quanuuid;
	}
	
	public String getModulename(){
		return modulename;
	}
	public void setModulename(String modulename){
		this.modulename = modulename;
	}

	/**
	 * @return the count
	 */
	public int getCount() {
		return count;
	}

	/**
	 * @param count the count to set
	 */
	public void setCount(int count) {
		this.count = count;
	}

	/**
	 * @return the memberList
	 */
	public RecordSet getMemberList() {
		return memberList;
	}

}
